﻿
using NineSeas.Models;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.Security;
namespace NineSeas.Drivers
{
    public class AuthorProfileDriver : ContentPartDriver<AuthorProfilePart>
    {
        public AuthorProfileDriver()
        {
        }

        protected override DriverResult Display(
            AuthorProfilePart part, string displayType, dynamic shapeHelper)
        {
            return ContentShape("Parts_AuthorProfile",
                () => shapeHelper.Parts_AuthorProfile(
                        DisplayName: part.DisplayName,
                        StatusMessage: part.StatusMessage,
                        Phone: part.Phone,
                        DateOfBirth: part.DateOfBirth,
                        BannerColor: part.BannerColor
                    ));
        }

        //GET
        protected override DriverResult Editor(AuthorProfilePart part, dynamic shapeHelper)
        {
            return ContentShape("Parts_AuthorProfile_Edit",
                () => shapeHelper.EditorTemplate(
                    TemplateName: "Parts/AuthorProfile",
                    Model: part,
                    Prefix: Prefix));
        }

        //POST
        protected override DriverResult Editor(
            AuthorProfilePart part, IUpdateModel updater, dynamic shapeHelper)
        {
            if (string.IsNullOrEmpty(part.DisplayName))
            {
                var user = part.As<IUser>();

                if (user != null)
                {
                    part.DisplayName = user.UserName;
                }
            }

            updater.TryUpdateModel(part, Prefix, null, null);
            return Editor(part, shapeHelper);
        }
    }
}